<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/Panda.png" alt="">
        <span>后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <!-- 主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-btn" @click="toggleCollapse"><i :class="isCollapse ? 'el-icon-s-fold' : 'el-icon-s-unfold'"></i></div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :default-active="activePath"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          unique-opened>
          <!-- 一级菜单 -->
          <el-submenu :index="item1.id.toString()" v-for="item1 in menuList" :key="item1.id">
            <!-- 一级菜单的模块区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item1.id]"></i>
              <!-- 文本 -->
              <span>{{ item1.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="item2.path" v-for="item2 in item1.children" :key="item2.id" @click="saveNavState(item2.path); saveNavTitle(item1.authName, item2.authName)">
              <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ item2.authName }}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{ breadcrumb.fatherTitle }}</el-breadcrumb-item>
          <el-breadcrumb-item>{{ breadcrumb.title }}</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      breadcrumb: {
        fatherTitle: '',
        title: ''
      },
      isCollapse: false, // 是否折叠
      menuList: [], // 左侧菜单数据
      iconsObj: {
        125: 'iconfont icon-yonghuguanli',
        103: 'iconfont icon-kucun',
        101: 'iconfont icon-chanpin',
        102: 'iconfont icon-dingdanliebiao',
        145: 'iconfont icon-baobiaozhongxin'
      },
      activePath: '' // 被激活的链接地址
    }
  },
  methods: {
    logout () {
      // 清空token
      window.sessionStorage.clear()
      // 重定向到登录页面
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 点击按钮的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    },
    saveNavTitle (fatherTitle, title) {
      this.breadcrumb.fatherTitle = fatherTitle
      this.breadcrumb.title = title
      window.sessionStorage.setItem('fatherTitle', fatherTitle)
      window.sessionStorage.setItem('title', title)
    }
  },
  mounted () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
    this.breadcrumb.fatherTitle = window.sessionStorage.getItem('fatherTitle')
    this.breadcrumb.title = window.sessionStorage.getItem('title')
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  background-color: #373d41;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    height: 55px;
    img {
      height: 100%;
    }
    span {
      padding-left: 15px;
      color: #fff;
      font-size: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .toggle-btn {
    color: #fff;
    font-size: 14px;
    text-align: center;
    line-height: 26px;
    background-color: #4a5064;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
  }
  .iconfont {
    margin-right: 10px;
  }
}
.el-main {
  background-color: #e9edf0;
  .el-breadcrumb {
    margin-bottom: 15px;
    font-size: 12px;
  }
}

</style>
